import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.jdom.*;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class Main {

	public static void main(String[] args) {		
		writeXML();
		readXML();
	}
	
	public static void writeXML() {
		Element root = new Element("wurzel"); //<root> & </root>
		Element mensch = new Element("mensch"); //<mensch> & </mensch>
		Element name = new Element("name").setText("Meyer"); //<name>Meyer</name>
		Element vorname = new Element("vorname").setText("Micheal"); //<vorname>Micheal</vorname>
		root.addContent(mensch.addContent(name).addContent(vorname)); //Hirarchie aufbauen: Wurzel->Mensch->Name & Vorname
		
		Document doc = new Document(root);
		try {
			FileOutputStream out = new FileOutputStream("Text.xml");
			XMLOutputter serializer = new XMLOutputter(Format.getPrettyFormat());
			serializer.output(doc, out);
			
		} catch(FileNotFoundException e) {
			e.printStackTrace();
		} catch(IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void readXML() {
		try {
			SAXBuilder builder = new SAXBuilder();
			Document doc = builder.build("Text.xml");
			
			//root element herauslesen
			Element root=doc.getRootElement();
			Element mensch = root.getChild("mensch");
			
			String name = mensch.getChildText("name");
			String vorname = mensch.getChildText("vorname");
			System.out.println(vorname + " " + name);
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

